package com.rundow.common.sdk.bean.driver;

import cn.hutool.core.util.ObjectUtil;
import com.rundow.common.base.exception.NotFoundException;
import com.rundow.common.sdk.service.DriverCommandService;
import com.rundow.common.sdk.service.DriverCustomService;
import com.rundow.common.sdk.service.DriverScheduleService;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;

@Component
public class ServiceContainerContext extends ConcurrentHashMap<String, ServiceContainerContext.ServiceContainer> {

    public static String currentDriverName;

    public static ServiceContainer currentServiceContainer;

    public static DriverCustomService currentCustomService;
    public static DriverCommandService currentCommandService;
    public static DriverScheduleService currentScheduleService;

    public String getCurrentDriverName(String driverName) {
        if (containsKey(driverName)) {
            currentDriverName = driverName;
            return driverName;
        }
        throw new NotFoundException(" this driver {} is  not found in ServiceContainerContext", driverName);
    }

    public ServiceContainer getCurrentServiceContainer(String driverName) {
        currentServiceContainer = get(getCurrentDriverName(driverName));
        if (ObjectUtil.isNull(currentServiceContainer)) {
            throw new NotFoundException(" this driver {} is  not found in ServiceContainerContext", driverName);
        }
        return currentServiceContainer;
    }

    public DriverCustomService getCurrentCustomService(String driverName) {
        currentCustomService = getCurrentServiceContainer(driverName).getCustomService();
        return currentCustomService;
    }

    public DriverCommandService getCurrentCommandService(String driverName) {
        currentCommandService = getCurrentServiceContainer(driverName).getCommandService();
        return currentCommandService;
    }

    public DriverScheduleService getCurrentScheduleService(String driverName) {
        currentScheduleService = getCurrentServiceContainer(driverName).getScheduleService();
        return currentScheduleService;
    }


    @Getter
    @Setter
    @NoArgsConstructor
    @AllArgsConstructor
    public static class ServiceContainer {

        private DriverCustomService customService;

        private DriverCommandService commandService;

        private DriverScheduleService scheduleService;
    }

}
